package com.cupster.android_x_frame;

import android.content.pm.PackageManager;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class DetectionActivity extends AppCompatActivity implements GestureOverlayView.OnGesturePerformedListener {

    GestureOverlayView mGestureOverlayView;
    TextView mTvHint;

    GestureLibrary mGestureLibrary;
    File mFileGesture = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_draw_gesture);
        mGestureOverlayView = findViewById(R.id.gesture_overlay);
        mTvHint = findViewById(R.id.text);

        mGestureOverlayView.setFadeOffset(800);//默认420ms,设置为1600ms
        mGestureOverlayView.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);//默认单指连续绘制，设置为多指绘制
        mGestureOverlayView.setGestureStrokeWidth(10);//绘制线宽
        mGestureOverlayView.setGestureColor(Color.parseColor("#3ca1f9"));//绘制颜色
        mGestureOverlayView.setUncertainGestureColor(Color.parseColor("#a7dbf7"));//渐成色
        //设置监听
        mGestureOverlayView.addOnGesturePerformedListener(this);
        //1.文件方式
        PermissionUtil.applyPermission(this ,101 ,BuildConfig.APPLICATION_ID);
    }

    @Override
    protected void onDestroy() {
        mGestureOverlayView.removeOnGesturePerformedListener(this);
        super.onDestroy();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 101:
                if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    initStoreFile();//外部存储方式
                }else {
                    Toast.makeText(this, "权限限制，可能无法正常使用主要功能", Toast.LENGTH_SHORT).show();
                    PermissionUtil.skipPermissionSetting(this);
                }
                break;
            default:
                break;
        }
    }

    private void initStoreFile() {
        if (mFileGesture == null && Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            mFileGesture = new File(Environment.getExternalStorageDirectory(), "gesture");
        }
        if (mGestureLibrary == null ){
            mGestureLibrary = GestureLibraries.fromFile(mFileGesture);//方式1
//            mGestureLibrary = GestureLibraries.fromPrivateFile(this ,Environment.getExternalStorageDirectory()+"gesture");//方式2
//            mGestureLibrary = GestureLibraries.fromRawResource(this ,R.raw.gesture);//方式3
            boolean flag = mGestureLibrary.load();
            mTvHint.append("\n");
            mTvHint.append("load  = "+flag);
        }
    }

    @Override
    public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) {
        mTvHint.append("\n");
        mTvHint.append("--> 所有绘制路径，识别完成");

        initStoreFile();//校验

        mGestureLibrary.setOrientationStyle(4);

        ArrayList<Prediction> predictions = mGestureLibrary.recognize(gesture);
        mTvHint.append("\n");
        mTvHint.append("识别数量：--> "+predictions.size());
        for (Prediction p : predictions){
            mTvHint.append("\n");
            mTvHint.append(p.name+"匹配值--> "+p.score);
            if (p.score >3.0){//匹配阈值值越大，要求匹配精度更高
                mTvHint.append("\n");
                mTvHint.append("匹配到--> "+p.name);

                doSomeThing();//触发事件
//                break;//实际使用时，放开注释，此处为测试显示其他不匹配手势 & 识别结果
            }else {
                mTvHint.append("\n");
                mTvHint.append(p.name+"--> 不匹配");
            }
        }
    }

    private void doSomeThing() {
        mTvHint.append("\n");
        mTvHint.append("===================\n");
        mTvHint.append("触发-->调用事件");
        mTvHint.append("\n");
        mTvHint.append("===================");
    }
}
